﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 7. Write a script that parses an URL address given in the format:
			
			[protocol]://[server]/[resource]

			and extracts from it the [protocol], [server] and [resource] elements.
			Return the elements in a JSON object.

			For example from the URL http://www.devbg.org/forum/index.php
			the following information should be extracted:

			{protocol: "http",
			server: "www.devbg.org", 
			resource: "/forum/index.php"}  -->
	<style>
		textarea, button {
			display: block;
			width: 800px;
		}
	</style>
</head>
<body>
	<textarea id="input" placeholder="Text to change"></textarea>
	<button onclick="doExtract()">Extract</button>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		function extract(input) {
			var res = new Array();
			res.push('{protocol: "');

			// Get protocol
			if (input.indexOf("://") >= 0) {
				var arr = input.split("://");
				res.push(arr[0]);
				input = arr[1];
			}

			res.push('",</br>&nbsp;server: "');

			// Get server
			var index = input.indexOf('/');
			if (index >= 0) {
				res.push(input.substring(0, index));
				input = input.substr(index);
			}

			res.push('",</br>&nbsp;resource: "');

			// Get resources (what is left)
			res.push(input);

			res.push('"}');

			return res.join('');
		}

		// Button OnClick
		function doExtract() {
			// Read User input
			var input = jsConsole.read("#input");

			// Output
			jsConsole.writeLine("You have entered:");
			jsConsole.writeLine("<blockquote>" + input + "</blockquote>");

			// Replace all spaces with &nbsp;
			var result = extract(input);
			jsConsole.writeLine("Result:");
			jsConsole.writeLine("<blockquote>" + result + "</blockquote>");
		}
	</script>
</body>
</html>
